package com.terrier.ui.controls
{
    import com.terrier.ui.core.IWxDataRenderer;
    import com.terrier.utils.WxFunctionUtility;

    /**
     * 列表组件渲染器的默认工厂
     * @author Jarvis.weng
     */
    public class WxListRendererDefaultFactory implements IWxListRendererFactory
    {
        protected var _rendererClass:Class;

        protected var _onFactoryChange:Function;

        protected var _dataLength:int;

        /**
         * 构造函数
         */
        public function WxListRendererDefaultFactory()
        {
            initialize();
        }

        protected function initialize():void
        {
            rendererClass = WxLabel;
        }

        public function get dataLength():int
        {
            return _dataLength;
        }

        public function set dataLength(value:int):void
        {
            _dataLength = value;
        }

        /**
         *
         * @param index
         * @param data
         * @return
         */
        public function createRenderer(index:int, data:Object):IWxDataRenderer
        {
            var renderer:IWxDataRenderer = new _rendererClass();
            renderer.data = data;
            return renderer;
        }

        /**
         *
         * @param renderer
         */
        public function disposeRenderer(renderer:IWxDataRenderer):void
        {
            // nothing to do.
        }

        /**
         *
         * @return
         */
        public function get rendererClass():Class
        {
            return _rendererClass;
        }

        /**
         *
         * @param value
         */
        public function set rendererClass(value:Class):void
        {
            if (value == null)
            {
                throw new ArgumentError("renderer class can not be null.");
                return;
            }

            _rendererClass = value;

            WxFunctionUtility.safeCall(onFactoryChange);
        }

        public function get onFactoryChange():Function
        {
            return _onFactoryChange;
        }

        public function set onFactoryChange(value:Function):void
        {
            _onFactoryChange = value;
        }
    }
}
